<?php

/**
 * @Author: Wang Chunsheng 2192138785@qq.com
 * @Date:   2020-03-02 08:13:49
 * @Last Modified by:   Wang Chunsheng 2192138785@qq.com
 * @Last Modified time: 2020-03-10 16:46:07
 */


namespace app\modules\diandi_shop\controllers\goods;

use Yii;
use app\modules\diandi_shop\models\DdCategory;
use app\modules\diandi_shop\models\searchs\DdCategorySearch;
use backend\controllers\BaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\components\Upload;
use yii\data\ActiveDataProvider;
use yii\web\Response;

/**
 * DdCategoryController implements the CRUD actions for DdCategory model.
 */
class DdCategoryController extends BaseController
{

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all DdCategory models.
     * @return mixed
     */
    public function actionIndex()
    {

        $searchModel = new DdCategorySearch();
        //        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $query = DdCategory::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => false
        ]);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single DdCategory model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new DdCategory model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new DdCategory();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->category_id]);
        }
        $catedata = $model::find()->where(['parent_id' => 0])->asArray()->all();
        array_unshift($catedata, ['category_id' => 0, 'name' => '顶级分类']);
        return $this->render('create', [
            'model' => $model,
            'catedata' => $catedata,
        ]);
    }

    /**
     * Updates an existing DdCategory model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->category_id]);
        }
        $catedata = $model::find()->where(['parent_id' => 0])->asArray()->all();
        array_unshift($catedata, ['category_id' => 0, 'name' => '顶级分类']);
        return $this->render('update', [
            'model' => $model,
            'catedata' => $catedata,
        ]);
    }

    /**
     * Deletes an existing DdCategory model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * @return string
     */
    public function actionChildcate()
    {
        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            $data = Yii::$app->request->post();
            $parent_id = $data['parent_id'];
            $cates = DdCategory::findAll(['parent_id' => $parent_id]);

            return $cates;
        }
    }

    /**
     * Finds the DdCategory model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return DdCategory the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = DdCategory::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
